package com.xlr.excel.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xlr.excel.core.SourseVehicleCoreService;
import com.xlr.excel.entity.SourseVehicle;
import com.xlr.excel.queue.QueueHander;
import com.xlr.excel.service.SourseVehicleService;
import com.xlr.excel.task.QueueTaskThread;
import com.xlr.excel.util.result.RestResult;
import com.xlr.excel.util.result.RestResultBuilder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.BlockingQueue;

/**
 * @Author: xlr
 * @Date: Created in 下午8:25 2019/2/9
 */
@RestController
@Data
@Slf4j
public class ExcelController {


    @Autowired
    private SourseVehicleCoreService sourseVehicleCoreService;


    @Autowired
    private SourseVehicleService sourseVehicleService;
    /**
     *
     * @param file
     *@param type 1 千条数据 其他为非千条数据
     * @return
     */
    @PostMapping("fileUpload")
    public RestResult uploudExcel(MultipartFile file,Integer type){
        try {
            if(type.equals( "1" )){
                BlockingQueue <Object> sv = QueueHander.getQueue( "sv" );
                sv.clear();
                QueueTaskThread.setSize(999);
            }else {
                QueueTaskThread.setSize(1);
            }
            sourseVehicleCoreService.uploadExcel( file );
            return RestResultBuilder.builder().success().build();
        }catch (Exception e){
            e.printStackTrace(  );
            return RestResultBuilder.builder().failure().message( e.getMessage() ).build();
        }
    }
    @GetMapping("count")
    public RestResult findCount(){
        try {
            Integer count = sourseVehicleCoreService.findCount();
            return RestResultBuilder.builder().success(count).build();
        }catch (Exception e){
            e.printStackTrace(  );
            return RestResultBuilder.builder().failure().message( e.getMessage() ).build();
        }
    }
    /**
     *
     * 文件导出
     * @param response
     * @return
     */
    @GetMapping("dowloadExcel")
    public RestResult dowloudExcel(@RequestParam("dimension") String dimension , HttpServletResponse response){
        try {
            sourseVehicleCoreService.dowloadExcel( dimension,response );
            return RestResultBuilder.builder().success().build();
        }catch (Exception e){
            e.printStackTrace(  );
            return RestResultBuilder.builder().failure().message( e.getMessage() ).build();
        }
    }

    /**
     * 清空数据库
     * @return
     */
    @GetMapping("cleanDB")
    public RestResult cleanDB(){
        Wrapper<SourseVehicle> wrapper =new EntityWrapper(  );
        wrapper.eq( "delete_state",1 );
        sourseVehicleService.delete( wrapper );
        return RestResultBuilder.builder().success().build();
    }
}
